from sqlalchemy import Column, Integer, String, Boolean, DateTime, Text, JSON, ForeignKey, Enum, Numeric
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from app.core.database import Base
import enum


class ProjectStatus(str, enum.Enum):
    DRAFT = "draft"
    SUBMITTED = "submitted"
    UNDER_REVIEW = "under_review"
    APPROVED = "approved"
    REJECTED = "rejected"


class Project(Base):
    __tablename__ = "projects"

    id = Column(Integer, primary_key=True, index=True)
    title = Column(String(200), nullable=False)
    description = Column(Text)
    short_description = Column(String(500))
    
    # 关联信息
    competition_id = Column(Integer, ForeignKey("competitions.id"), nullable=False)
    team_id = Column(Integer, ForeignKey("teams.id"))
    creator_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    
    # 项目内容
    repository_url = Column(String(500))  # GitHub等代码仓库
    demo_url = Column(String(500))  # 演示链接
    documentation_url = Column(String(500))  # 文档链接
    
    # 技术栈和标签
    technologies = Column(JSON, default=list)  # ["Python", "React", "TensorFlow"]
    tags = Column(JSON, default=list)
    
    # 文件和媒体
    files = Column(JSON, default=list)  # 上传的文件列表
    images = Column(JSON, default=list)  # 项目截图
    video_url = Column(String(500))  # 演示视频
    
    # 评分和排名
    score = Column(Numeric(5, 2))  # 总分
    ranking = Column(Integer)  # 排名
    judges_scores = Column(JSON, default=dict)  # 评委评分详情
    
    # 状态和元数据
    status = Column(Enum(ProjectStatus), default=ProjectStatus.DRAFT)
    is_public = Column(Boolean, default=True)
    view_count = Column(Integer, default=0)
    like_count = Column(Integer, default=0)
    
    # 提交信息
    submitted_at = Column(DateTime(timezone=True))
    submission_notes = Column(Text)
    
    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    
    # 关系
    competition = relationship("Competition", back_populates="projects")
    team = relationship("Team", back_populates="projects")
    creator = relationship("User", back_populates="projects")
    comments = relationship("ProjectComment", back_populates="project")


class ProjectComment(Base):
    __tablename__ = "project_comments"

    id = Column(Integer, primary_key=True, index=True)
    project_id = Column(Integer, ForeignKey("projects.id"), nullable=False)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    content = Column(Text, nullable=False)
    
    # 评论类型
    is_judge_comment = Column(Boolean, default=False)  # 是否为评委评论
    rating = Column(Integer)  # 1-5星评分
    
    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    
    # 关系
    project = relationship("Project", back_populates="comments")
    user = relationship("User")